<?php

require_once 'include/Image.class.php';
require_once 'include/DataBase.class.php';

function get($name = null) {
    $get = ($name == null) ? $name : (isset($_GET[$name]) ? $_GET[$name] : null);
    if ($get == null) {
        $queryString = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);
        $queryString = explode("&", $queryString);
        foreach ($queryString as $qstring) {
            $qstring = explode("=", $qstring);
            if ($qstring[0] == $name) {
                return $qstring[1];
            }
        }
    }
    return $get;
}

$data['media'] = @$_FILES['image'];
$data['action'] = get("action");
$data['bucket'] = get("bucket");
$data['targetId'] = get("targetId");
$data['code'] = get("code");
$data['width'] = get("width");
$data['url'] = get("url");
$data['height'] = get("height");
if ($data['bucket'] == null || $data['bucket'] == '') {
    die(json_encode(["success" => false, "message" => "Bucket chưa được nhập"]));
}
$image = new Image();
if (!$image->loadBucket($data['bucket'])) {
    die(json_encode(["success" => false, "message" => "Tạo bucket trên hệ thống bị lỗi"]));
}
switch ($data['action']) {
    case 'upload':
        $image->upload($data);
        break;
    case 'dowload':
        $image->dowload($data);
        break;
    case 'delete':
        $image->delete($data);
        break;
    case 'get': default :
        $image->get($data);
        break;
}
?>